package com.cjq.wx.Controller;


import com.cjq.bean.BootStrapTableExpress;
import com.cjq.bean.Express;
import com.cjq.bean.Message;
import com.cjq.bean.WxUser;
import com.cjq.mvc.ResponseBody;
import com.cjq.mvc.ResponseView;
import com.cjq.service.ExpressService;
import com.cjq.util.DateFormatUtil;
import com.cjq.util.JSONUtil;
import com.cjq.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class QRCodeController {

    //有可能是直接在主页点击我的二维码  有可能是在我的未取件快递中点击    最后将是哪个入口的信息   电话/取件码  保存在session 最后跳转到显示二维码页面
    @ResponseView("/wx/createQrcode.do")
    public String createQrcode(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        String type = req.getParameter("type");
        String phone = null;
        String qRCodeContent = null;    //作为二维码展示的信息
        if ("express".equals(type)){
            //快递二维码:  被快递员扫之后，展示单个快递信息
            qRCodeContent = "express_"+code;
        }else{
            //用户的二维码 ：展示用户所有的快递
            WxUser wxUser = UserUtil.getWxUser(req.getSession());
            phone = wxUser.getPhone();
            qRCodeContent = "userPhone_"+phone;
        }
        req.getSession().setAttribute("qrcode",qRCodeContent);

        return "/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message msg  = new Message();
        if (qrcode ==null){
            msg.setStatus(-1);
            msg.setResult("二维码获取出错，请用户重新操作");
        }
        msg.setStatus(0);
        msg.setResult(qrcode);
        return JSONUtil.toJSON(msg);
    }


    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        System.out.println(code);
        boolean flag = ExpressService.updateStatus(code);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("取件码不存在，请用户更新二维码");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        Express e = ExpressService.findByCode(code);

        Message msg = new Message();
        if (e==null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getInTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            BootStrapTableExpress e1 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),e.getCode(),inTime,outTime,status,e.getSysPhone());
            msg.setData(e1);
        }
        return JSONUtil.toJSON(msg);
    }



}
